AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( 'shared.lua' )

function ENT:Initialize()

	self.Entity:SetModel( "models/props_interiors/VendingMachineSoda01a.mdl" ) 
	self.Entity:SetName("SMG1 Vendor")
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )
	self.Entity:SetUseType(SIMPLE_USE)
	
	local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
		phys:EnableGravity(true)
		phys:EnableDrag(true)
		phys:EnableCollisions(true)
	end
	self.Entity:SetKeyValue("rendercolor", "255 255 255")
end

function ENT:SpawnFunction( ply, tr )

	if ( !tr.Hit ) then return end
	
	local SpawnPos = tr.HitPos + tr.HitNormal * 16 + Vector(0,0,50)
	
	local ent = ents.Create( "kr_smg" )
	ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Initialize()
	ent:Activate()
	ent.SPL = ply
	return ent
end

function ENT:Use(activator,caller)
	if not (activator and activator:IsValid() and activator:IsPlayer()) then return end
	local NPCKillCount = activator:GetNetworkedInt("npckillcount") or 0
	if not (NPCKillCount) then
		NPCKillCount = 0
	end
	if NPCKillCount >= 6 then
		local vial = ents.Create("weapon_smg1")
		vial:SetPos(self.Entity:LocalToWorld(Vector(22,0,-24)))
		vial:Spawn()
		if vial and vial:IsValid() then
			activator:SetNetworkedInt("npckillcount",NPCKillCount-6)
		end
	end
end